﻿using UnityEngine;
using System.Collections;

public class RocketMovementControlBehaviour : MonoBehaviour {
	
	//Button mappings:
	public string horizontalAxis = "Horizontal";
	public string verticalAxis = "Vertical";

	public string trustButton = "Trust";

	//What is the max speed?:
	public float maxAcceleration = 15F;


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		float h = Input.GetAxis (horizontalAxis);
		float v = Input.GetAxis (verticalAxis);

		if ((h != 0.0f) || (v != 0.0f)) {
			//Rotera skeppet:
			//TODO: Här måste vi vara lite intelligenta med om man släpper knappen - är det så att man vill svänga eller?

			//			Vector2 vector = new Vector2 (Input.GetAxis (horizontalAxis), Input.GetAxis (verticalAxis));
			rigidbody2D.transform.rotation = Quaternion.AngleAxis (Mathf.Atan2 (v, h) * 57.2957795f, Vector3.forward);
		}
		if (Input.GetButton (trustButton)) {
			float vinkel = rigidbody2D.transform.rotation.eulerAngles.z;
			float x = Mathf.Cos(vinkel);
			float y = Mathf.Sin (vinkel);
			Vector2 riktning = new Vector2(x, y);
			rigidbody2D.AddForce (Vector2.ClampMagnitude(riktning, maxAcceleration));
		}
	}
}
